package com.zd.wechatPlatform.common.utils;

import com.alibaba.fastjson.JSONObject;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.springframework.util.StringUtils;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.servlet.http.HttpServletRequest;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

public class XMLUtil
{
	public static void parseElement(Element elt, Map<String, String> map)
	{
		if ((elt.getText() != null) && (!"".equals(elt.getText()))) {
			map.put(elt.getName(), elt.getText());
		}
		if (elt.getChildren().size() == 0) {
			return;
		}
		Iterator<?> iter = elt.getChildren().iterator();
		while (iter.hasNext())
		{
			Element childElt = (Element)iter.next();
			parseElement(childElt, map);
		}
	}

	public static Map<String, String> parseXmlFromInputStreamOld(InputStream is)
			throws Exception
	{
		Map<String, String> map = new HashMap<String, String>();
		Document doc = null;
		SAXBuilder sb = new SAXBuilder();
		doc = sb.build(is);
		Element rootElt = doc.getRootElement();
		parseElement(rootElt, map);
		return map;
	}

	public static Map<String, String> parseXmlFromInputStream(InputStream is)
			throws Exception
	{
		Map<String, String> map = new HashMap<String, String>();
		String str = IOUtils.toString(is, "UTF-8");
		LogUtils.debug("xmlStr:\n" + str);
		if (!StringUtils.isEmpty(str))
		{
			byte[] b = str.getBytes();
			InputStream inp = new ByteArrayInputStream(b);
			SAXBuilder sb = new SAXBuilder();
			Document doc = sb.build(inp);
			Element rootElt = doc.getRootElement();
			parseElement(rootElt, map);
			LogUtils.debug("map:" + JSONObject.toJSONString(map));
		}
		return map;
	}

	public static String getXmlStrFromRequest(HttpServletRequest request) throws IOException {
		String result = "";
		InputStream inStream = request.getInputStream();
		int _buffer_size = 1024;
		if (inStream != null) {
			ByteArrayOutputStream outStream = new ByteArrayOutputStream();
			byte[] tempBytes = new byte[_buffer_size];
			int count = -1;
			while ((count = inStream.read(tempBytes, 0, _buffer_size)) != -1) {
				outStream.write(tempBytes, 0, count);
			}
			tempBytes = null;
			outStream.flush();
			//将流转换成字符串
			result = new String(outStream.toByteArray(), "UTF-8");
			LogUtils.debug("result:" + result);
		}
		return result;
	}

	public static Map<String, String> xmlToMap(String strXML) throws Exception {
		HashMap<String, String> data = new HashMap<String, String>();
		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
		ByteArrayInputStream stream = new ByteArrayInputStream(strXML.getBytes("UTF-8"));
		org.w3c.dom.Document doc = documentBuilder.parse(stream);
		doc.getDocumentElement().normalize();
		NodeList nodeList = doc.getDocumentElement().getChildNodes();

		for(int idx = 0; idx < nodeList.getLength(); ++idx) {
			Node node = nodeList.item(idx);
			if(node.getNodeType() == 1) {
				org.w3c.dom.Element element = (org.w3c.dom.Element)node;
				data.put(element.getNodeName(), element.getTextContent());
			}
		}

		try {
			stream.close();
		} catch (Exception var10) {
			;
		}

		return data;
	}
}
